library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity state_control is
port(
clk:in std_logic;
clk_second:in std_logic;
turn_left,turn_right,recover,stop,poweroff:in std_logic;
--count_down:in integer range 0 to 9;
state:out integer range 0 to 4;--0:opening music,1:running,2:turn_left,3:turn_right,4:stop
--state_save:out integer range 0 to 4
save_flag:out std_logic
);
end state_control;

architecture behav of state_control is
signal state_tmp,state_before:integer range 0 to 4;
signal count_down:integer range 0 to 11;
signal reset_flag:std_logic;
signal state_save:integer range 0 to 4;

begin 
p1:process(clk,state_tmp,turn_left,turn_right,recover,stop,count_down,poweroff)--state transfer
	begin
	if(poweroff='0') then
		if(clk'event and clk='1') then
		case state_tmp is
			when 0=>
			if count_down=4 then
				state_tmp<=1;reset_flag<='0';save_flag<='0';
			end if;
			
			when 1=>
			state_before<=1;state_save<=1;
			if (turn_left='1' and turn_right='0' and stop='0') then
				state_tmp<=2;reset_flag<='1';save_flag<='1';
			elsif (turn_right='1' and turn_left='0' and stop='0') then
				state_tmp<=3;reset_flag<='1';save_flag<='1';
			elsif (stop='1' and turn_left='0' and turn_right='0') then 
				state_tmp<=4;reset_flag<='1';save_flag<='1';
			else 
				state_tmp<=1;reset_flag<='0';save_flag<='0';
			end if;
				
			when 2=>
			state_before<=2;state_save<=2;
			if (recover='1' and stop='0' and count_down/=11) then
				state_tmp<=1;reset_flag<='0';save_flag<='1';
			elsif (stop='1' and recover='0' and count_down/=11)then
				state_tmp<=4;reset_flag<='1';save_flag<='1';
			elsif (count_down=11 and stop='0' and recover='0') then
				state_tmp<=1;reset_flag<='0';save_flag<='1';
			else 
				state_tmp<=2;reset_flag<='0';save_flag<='0';
			end if;
			
			when 3=>
			state_before<=3;state_save<=3;
			if (recover='1' and stop='0' and count_down/=11) then
				state_tmp<=1;reset_flag<='0';save_flag<='1';
			elsif (stop='1' and recover='0' and count_down/=11)then
				state_tmp<=4;reset_flag<='1';save_flag<='1';
			elsif (count_down=11 and stop='0' and recover='0') then
				state_tmp<=1;reset_flag<='0';save_flag<='1';
			else 
				state_tmp<=3;reset_flag<='0';save_flag<='0';
			end if;
				
			when 4=>
			state_before<=4;state_save<=4;
			if (recover='1' and count_down/=6) then 
				state_tmp<=1;reset_flag<='0';save_flag<='1';
			elsif (recover='0' and count_down=6) then
				state_tmp<=1;reset_flag<='0';save_flag<='1';
			else 
				state_tmp<=4;reset_flag<='0';save_flag<='0';
			end if;
			end case;
			end if;
			
				
		end if;
end process p1;	

p2:process(clk,clk_second,state_tmp,reset_flag)
begin
	if (reset_flag='1') then
		count_down<=0;
		
	--if(clk'event and clk='1') then
		--if(state_before/=state_tmp) then	
			--count_down<=0;
		--end if;
	--end if;
	elsif(clk_second'event and clk_second='1') then
		--if(state_before/=state_tmp) then	
			--count_down<=0;
		--end if;
		--if (reset_flag='1') then
		--count_down<=0;
		--end if;
		if(state_tmp=2 or state_tmp=3 or state_tmp=0 or state_tmp=4) then
			if count_down=11 then
				count_down<=0;
			else
			count_down<=count_down+1;
			end if;
		end if;
	end if;
end process p2;
		
state<=state_tmp;
end behav;